package com.weyoung.app.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.weyoung.app.dto.req.FamilyCultureDTO;
import com.weyoung.app.dto.req.ModFamTraDTO;
import com.weyoung.app.service.FamilyCultureService;
import com.weyoung.app.service.FamilyService;
import com.weyoung.common.consatant.result.Result;
import com.weyoung.common.model.dto.app.CreateFamilyDTO;
import com.weyoung.common.model.entity.FamilyCulture;
import com.weyoung.common.model.vo.app.FamilyVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/family")
@Tag(name = "APP端-家族接口")
@Slf4j
@RequiredArgsConstructor
public class FamilyController {

    private final FamilyService familyService;

    private final FamilyCultureService familyCultureService;

    @PostMapping("/getFamilyMotto")
    @Operation(summary = "查看家风")
    public Result getFamilyMotto() {
        return familyService.getFamilyMotto();
    }

    @PostMapping("/modifyFamilyMotto")
    @Operation(summary = "修改家风")
    public Result getFamilyTradition(@RequestBody ModFamTraDTO modFamTraDTO) {
        return familyService.modifyFamilyMotto(modFamTraDTO);
    }

//    @PostMapping("/addFamilyCulture")
//    @Operation(summary = "添加家族文化")
//    public Result addFamilyCulture(@RequestPart("data") FamilyCultureDTO familyCultureDTO,
//                                   @RequestPart(value = "coverImage",required = false) MultipartFile coverImage,
//                                   @RequestPart(value = "images",required = false) List<MultipartFile> images) {
//        return familyCultureService.addFamilyCulture(familyCultureDTO,coverImage,images);
//    }
    @PostMapping(value = "/addFamilyCulture", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result addFamilyCulture(
            @RequestPart("data") String data,
            @RequestPart(value = "coverImage", required = false) MultipartFile coverImage,
            @RequestPart(value = "images", required = false) List<MultipartFile> images) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        FamilyCultureDTO dto = mapper.readValue(data, FamilyCultureDTO.class);
        return familyCultureService.addFamilyCulture(dto, coverImage, images);
    }

    @PostMapping("/getFamilyCulture/{id}")
    @Operation(summary = "查看家族文化详情")
    public Result getFamilyCulture(@PathVariable("id") String id) {
        return familyCultureService.getFamilyCulture(id);
    }

    @PostMapping("/getFamilyCultureList")
    @Operation(summary = "查看家族文化列表")
    public Result getFamilyCultureList() {
        return familyCultureService.getFamilyCultureList();
    }

    @PostMapping("/addFamily")
    @Operation(summary = "创建族谱/家族")
    public Result<Void> addFamily(@RequestBody CreateFamilyDTO createFamilyDTO) {
        familyService.createFamily(createFamilyDTO);
        return Result.success();
    }

    @GetMapping("/getFamily")
    @Operation(summary = "获取族谱/家族")
    public Result<FamilyVO> getFamily() {
        return Result.success(familyService.getFamily());
    }

}
